//
//  CommonViewController.m
//  Weibo
//
//  Created by Anasue on 9/21/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "CommonViewController.h"
#import "SearchBar.h"
#import "CommonGroup.h"
#import "CommonItem.h"
#import "CommonCell.h"
#import "CommonArrowItem.h"
#import "CommonSwitchItem.h"
#import "CommonLabelItem.h"

@interface CommonViewController ()
@property (nonatomic, strong) NSMutableArray *groups;

@end

@implementation CommonViewController

- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = Color(211, 211, 211);
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = StatusCellInSet;
    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (id)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CommonGroup *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonCell *cell = [CommonCell cellWithTableView:tableView];
    CommonGroup *group = self.groups[indexPath.section];
    cell.item = group.items[indexPath.row];
    [cell setIndexPath:indexPath rowsOfSection:group.items.count];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonGroup *group = self.groups[indexPath.section];
    CommonItem *item = group.items[indexPath.row];
    if (item.destVcClass) {
        UIViewController *destVc = [[item.destVcClass alloc]init];
        destVc.title = item.title;
        [self.navigationController pushViewController:destVc animated:YES];
    }
    if (item.operation) {
        item.operation();
    }
}
@end
